function isLogin(){
    return localStorage.getItem("token")
}
//渲染导航栏
function renderTopbar(user){
    let photo = document.querySelector("#topbar-photo");
    let exit = document.querySelector("#exit");
    let currentUsername = document.querySelector("#currentUsername");
    photo.src = user.photo
    currentUsername.innerHTML = user.username
    //点击退出，返回登录页面
    exit.onclick = function(){
        localStorage.removeItem("token")
        location.href = "/code/admin/view/login/index.html";
    }
}
//渲染侧边菜单栏
function rendersidemenu(user,id){
    let regulate_news = document.querySelector(".regulate_news");
    let regulate_user = document.querySelector(".regulate_user");
    let userItem = document.querySelector(".userItem");
    let newsItem = document.querySelector(".newsItem");
    let icon = document.querySelectorAll(".icon-xiangxiajiantou");
    let arr2 = [userItem,newsItem];
    let arr1 =[regulate_user,regulate_news];
    for(let i = 0; i < arr1.length; i++) {
        arr1[i].onclick = function(){
            arr2[i].classList.toggle("active")
            icon[i].classList.toggle("icon_active")
        }
    }
    document.querySelector("#" + id).style.color = "#0a58ca";
    //如果登录的人不是管理员，就移除 用户管理 选项
    if(user.role !== "admin"){
        document.querySelector(".usermanage_container").remove()
    }
}
//登录成功，向其他页面请求资源
async function load(id){
    let user = JSON.parse(isLogin())
    if(user){
        let topbarText = await fetch("/code/admin/components/topbar/topbar.html").then(res => res.text())//请求回来的资源需要直接加载到页面，不能用json()解析
            document.querySelector(".topbar").innerHTML = topbarText
        let sidemenuText = await fetch("/code/admin/components/sidemenu/sidemenu.html").then(res =>res.text())
            document.querySelector(".sidemenu").innerHTML = sidemenuText   
    }else{//如果user信息为空则跳转到登录界面
        location.href = "/code/admin/view/login/index.html";
    }
    renderTopbar(user)
    rendersidemenu(user,id)

}
export {load,isLogin}